/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"')
      .replace(/\+/g, ' ') +
    '"}'
  )
}
 

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}

export function url_params (data, isPrefix) {

  isPrefix = isPrefix ? isPrefix : false

  let prefix = isPrefix ? '?' : ''

  let _result = []

  for (let key in data) {

    let value = data[key]

    // 去掉为空的参数

    if (['', undefined, null].includes(value)) {

      continue

    }

    if (value.constructor === Array) {

      value.forEach(_value => {

        _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))

      })

    } else {

      _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))

    }

  }



  return _result.length ? prefix + _result.join('&') : ''

}

export let xm_pkgs = [
  { label: "所有包", key: "" },
  // { label: "星月取图", key: "com.xinyuett.pkg" },
  // { label: "灵犀答题", key: "com.linxidt.pkg" },
  { label: "答题挑战", key: "com.datitiaoz.pkg" },
  { label: "趣味答题", key: "com.quweiwd.pkg" },
  { label: "汇拓", key: "com.deposittuoht.pkg"},
  { label: "万盛", key: "com.clockinws.pkg"},
  { label: "叶溪子", key: "com.yexiziyxz.pkg"},
  { label: "展恒", key: "com.zhanhengzh.pkg"},
  { label: "维泰", key: "com.weitai.pkg"},

  { label: "诚漫", key: "com.chengmancm.pkg"},
  { label: "昕恒", key: "com.xinhengxh.pkg"},
  { label: "易丰汇", key: "com.yifenghuiyfh.pkg" },
  { label: "晋源", key: "com.jinyuanxs.pkg" },
  { label: "小诺识字", key: "com.xiaonuosz.pkg" },
  { label: "元成", key: "com.yuancheng.pkg" },
  { label: "果享", key: "com.gx.wn" },
  { label: "泓丰", key: "com.honfeng.hf" },
  { label: "亿丰捷", key: "com.yifengjie.yfj" },
  { label: "铭雁", key: "com.mingyan.my" },
  { label: "圣熙", key: "com.shengxi.sx" },
  { label: "亿威", key: "com.yiwei.yw" },
  { label: "万鑫", key: "com.wanxin.wx" },
  { label: "科亚昌", key: "com.keyachang.kyc" },
  { label: "齐跃", key: "com.qiyue.qy" },
  { label: "翔远", key: "com.xianyuan.xy" },
  { label: "众鑫和", key: "com.zxh.zxh" },
  { label: "众鑫和华为", key: "com.zxh.zxhhw" },
  { label: "誉优盛", key: "com.yys.yys" },
  { label: "跃鸿泰", key: "com.yht.yht" },
  { label: "恒佳", key: "com.hj.hj" },
  // { label: "普兴越", key: "com.pxy.wn" },
  // { label: "海融", key: "com.hr.wn" },
  { label: "同润", key: "com.tr.wn" },
  { label: "鼎拓", key: "com.dt.wn" },
  { label: "宜利", key: "com.yl.wn" },
  { label: "锐嘉新", key: "com.rjx.wn" },
  { label: "盛铭", key: "com.sm.wn" },
  { label: "利锦源", key: "com.ljy.wn" },
  { label: "元嘉新越", key: "com.yjxy.wn" },
  { label: "禹创", key: "com.yc.wn" },
  { label: "清腾灏", key: "com.qth.wn" },
  // { label: "汇森达", key: "com.huisenda.da" },
  // { label: "源起汇", key: "com.yqh.hui" },
  // { label: "冠诚", key: "com.guancheng.gcpkg" },
  // { label: "创盈承", key: "com.cyc.wn" },
  // { label: "盛驰立", key: "com.scl.wn" },
  { label: "卓逸", key: "com.zhuoyi.zypkg" },
  { label: "华亦捷", key: "com.hyj.wn" },
]

export let fz_pkgs = [
  { label: "快悦读", key: "com.mttech.quickreader" },
  { label: "虾米剧场", key: "com.maotao.xiami" },
]
export let pkgs = xm_pkgs.concat(fz_pkgs)

export var qd_List = ['sanming','fuzhouqf']
